$(function(){
	$("#choosePro").click(function(){
		$("#portrait").click();
	});
});

$(function(){
	$("#portrait").change(function(){
		var file=$("#portrait").get(0).files[0];
	    var reader=new FileReader();
	    reader.onloadend=function (){
	        var dataURL = reader.result;
	        var img = $("#showPro")[0];
	        img.src = dataURL;// 插入到 DOM 中预览
	    };
	    reader.readAsDataURL(file); // 读出 base64
	});
});

$(function(){
	var skillClass=$("#skillClass");
	$("#yes").click(function(){
		skillClass.show("normal");
	});
	$("#no").click(function(){
		skillClass.hide("normal");
	});
});

//选择注册或登录
$(function(){
	$("#methodA").click(function(){
		$(this).addClass("choose");
		$(this).siblings("button").removeClass("choose");
		$("#signin").slideUp("normal");
		$("#information").slideDown("normal");
		$("#wait").hide("fast");
	});
	$("#methodB").click(function(){
		$(this).addClass("choose");
		$(this).siblings("button").removeClass("choose");
		$("#information").slideUp("normal");
		$("#signin").slideDown("normal");
		$("#wait").hide("fast");
		signCookie();
	});
	$("#methodA").click();
});

function signCookie(){
	var sign=$.cookie("sign");
    if(sign=="yes"){
		$("#yourName").val($.cookie("name"));
		$("#yourPass").val($.cookie("pass"));
	}
}

//注册验证
$(function(){
	$("#registerGo").click(function(){
		$("#wait").show("fast");
		var nickname=$("#nickname").val();
		var passwordA=$("#passwordA").val();
		var passwordB=$("#passwordB").val();
		var a=nickName(nickname);
		var b=passWord(passwordA,passwordB);
		var c;
		var provide=$("#provide input:checked").val();
		if(provide=="yes"){
			c=Skill();
		}else{
			c="no"
		};
		var pic=new FormData();
     	var img=$("#portrait").get(0).files[0];
     	pic.append("img",img);
		if(!a||!b||!c){
			$.mobile.silentScroll(0);
			$("#wait").hide("fast");
		}else{
			registerGo(nickname,passwordA,c,pic);
			$("#wait").hide("fast");
		}
	});
});

//检查昵称
function nickName(nick){
	var space=/\s/;
	var namePrompt=$("#namePrompt");
	namePrompt.attr("class","warning");
	if(nick==""){
		namePrompt.html("您的昵称:不能为空");
		return false;
	}else if(space.test(nick)){
		namePrompt.html("您的昵称:不能有空格");
		return false;
	}else{
		namePrompt.attr("class","");
		namePrompt.html("您的昵称:");
		return true;
	}
}

//检查密码
function passWord(wordA,wordB){
	var space=/\s/;
	var passPrompt=$("#passPrompt");
	passPrompt.attr("class","warning");
	if(wordA==""){
		passPrompt.html("设置密码:请输入密码");
		return false;
	}else if(wordB==""){
		passPrompt.html("设置密码:请确认密码");
		return false;
	}else if(space.test(wordA)||space.test(wordB)){
		passPrompt.html("设置密码:密码不能含有空格");
		return false;
	}else if(wordA!=wordB){
		passPrompt.html("设置密码:两次输入不一致");
		return false;
	}else{
		passPrompt.attr("class","");
		passPrompt.html("设置密码:");
		return true;
	}
}

//检查技能
function Skill(){
	var skill=[];
	var choose=$("#skillClass input");
	var legend=$("#skillClass legend");
	for(n in choose){
		if(choose[n].checked){
			skill.push(choose[n].value)
		}
	};
	if(skill.length==0){
		legend.attr("class","warning");
		legend.html("技能分类:请选择至少一项技能");
		return false;
	}else{
		legend.attr("class","");
		legend.html("技能分类:");
		return skill.join();
	}
}

//登录验证
$(function(){
	$("#signGo").click(function(){
		var yourName=$("#yourName").val();
		var yourPass=$("#yourPass").val();
		var whatName=$("#whatName");
		var whatPass=$("#whatPass");
		whatName.attr("class","warning");
		whatPass.attr("class","warning");
		var space=/\s/;
		var a,b;
		if(yourName==""){
			whatName.html("用户名:不能为空");
			a=false;
		}else if(space.test(yourName)){
			whatName.html("用户名:不能包含空格");
			a=false;
		}else{
			whatName.attr("class"," ");
			a=true;
		};
		if(yourPass==""){
			whatPass.html("密码:不能为空");
			b=false;
		}else if(space.test(yourPass)){
			whatPass.html("密码:不能包含空格");
			b=false;
		}else{
			whatPass.attr("class"," ");
			b=true;
		};
		if(a&&b){
			signGo(yourName,yourPass);
			$("#wait").show("fast");
		}
	});
});

//登录
function signGo(theName,thePass){
	$.ajax({
		type:"POST",
		url:"http://123.207.114.241/fishCow/php/login.php",
		dataType:"json",
		data:{
			name:theName,
			psw:thePass
		},
		success:function(x,y,z){
			if(!x){
				$("#wait").hide("fast");
			    $("#backInfo").html("登录失败，请核实登录信息");
			}else{
				var signAuto=$("#signAuto input:checked").val();
				$.cookie("userId",x,{expires:7});
				if(signAuto=="yes"){
					$.cookie("name",theName);
					$.cookie("pass",thePass);
					$.cookie("sign","yes");
				}else{
					$.cookie("sign","no");
				}
				$("#wait").show("fast");
				window.location.href="pages/findSkill.html";
			}
		},
		error:function(jqXHR){
		    $("#wait").hide("fast");
		    $("#backInfo").html("发生错误:"+jqXHR.status);
		}
	});
}

//注册
function registerGo(theName,thePass,theSkill,thePic){
	var register_state;
	if(theSkill=="no"){
		register=0;
	}else{
		register=1;
	};
	insertRegister(theName,thePass,theSkill,register);
	insertRegister2(theName,thePass,theSkill,register);
}

function insertRegister(theName,thePass,theSkill,register){
	$.ajax({
		type:"POST",
		url:"http://123.207.114.241/fishCow/php/insertRegister.php",
		dataType:"text",
		data:{
			nenufish_register_name:theName,
			nenufish_register_psw:thePass,
			// nenufish_register_pic:thePic,
			nenufish_register_state:register,
			nenufish_register_skill:theSkill
		},
		// processData:false,
		// contentType:false,
		success:function(x,y,z){
			console.log(x);
		},
		error:function(jqXHR){
		    console.log("发生错误"+jqXHR.status);
		    $("#wait").hide("fast");
		    $("#backInfo").html("发生错误"+jqXHR.status);
		}
	});
}

function insertRegister2(theName,thePass,theSkill,register){
	$.ajax({
		type:"POST",
		url:"http://123.207.114.241/fishCow/php/insertRegister2.php",
		data:{
			nenufish_register_name:theName,
			nenufish_register_psw:thePass,
			// nenufish_register_pic:thePic,
			nenufish_register_state:register,
			nenufish_register_skill:theSkill
		},
		// processData:false,
		// contentType:false,
		success:function(x,y,z){
			console.log(x);
		},
		error:function(jqXHR){
		    console.log("发生错误"+jqXHR.status);
		    $("#wait").hide("fast");
		    $("#backInfo").html("发生错误"+jqXHR.status);
		}
	});
}

// $.ajax({
// 	type:"POST",
// 	url:"http://123.207.114.241/fishCow/php/insertRegister.php",
// 	data:{
// 		nenufish_register_name:theName,
// 		nenufish_register_psw:thePass,
// 		nenufish_register_pic:thePic,
// 		nenufish_register_state:register,
// 		nenufish_register_skill:theSkill
// 	},
// 	processData:false,
// 	contentType:false,
// 	success:function(x,y,z){
// 		$.cookie("userId",x);
// 		$("#wait").hide("fast");
// 	    $("#backInfo").html("成功");
// 	    window.location.href="pages/findSkill.html"
// 	},
// 	error:function(jqXHR){
// 	    console.log("发生错误"+jqXHR.status);
// 	    $("#wait").hide("fast");
// 	    $("#backInfo").html("发生错误"+jqXHR.status);
// 	}
// });